package com.lv.common.widget.imageview

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.graphics.Region
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import com.lv.common.R

/**
 * 作者： ygx
 * 创建日期：2025/1/13
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：自定义的圆角imageview 抗锯齿
 */

class CustomRoundImage @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatImageView(context, attrs, defStyleAttr) {

    private val radii = floatArrayOf(0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f)
    private var mWidth = 0
    private var mHeight = 0
    private val mPath by lazy { Path() }
    private val mPath1 by lazy { Path() }
    private val mPaint by lazy {
        Paint().apply {
            color = Color.TRANSPARENT
            isAntiAlias = true
            style = Paint.Style.FILL
        }
    }

    private val mXfermode by lazy { PorterDuffXfermode(PorterDuff.Mode.SRC) }

    private val mRegion by lazy {
        Region().apply {
            setPath(mPath1, Region(0, 0, width, height))
        }
    }

    init {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null)
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.RadiusImageView)
        try {
            val radius = typedArray.getDimensionPixelSize(R.styleable.RadiusImageView_radius_all, 0)
            val leftTop =
                typedArray.getDimensionPixelSize(R.styleable.RadiusImageView_radius_leftTop, radius)
            val leftBottom = typedArray.getDimensionPixelSize(
                R.styleable.RadiusImageView_radius_leftBottom, radius
            )
            val rightTop = typedArray.getDimensionPixelSize(
                R.styleable.RadiusImageView_radius_rightTop, radius
            )
            val rightBottom = typedArray.getDimensionPixelSize(
                R.styleable.RadiusImageView_radius_rightBottom, radius
            )
            radii[0] = leftTop.toFloat()
            radii[1] = leftTop.toFloat()
            radii[2] = rightTop.toFloat()
            radii[3] = rightTop.toFloat()
            radii[4] = rightBottom.toFloat()
            radii[5] = rightBottom.toFloat()
            radii[6] = leftBottom.toFloat()
            radii[7] = leftBottom.toFloat()
        } finally {
            typedArray.recycle()
        }
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mWidth = w
        mHeight = h
        mPath.addRect(0f, 0f, mWidth.toFloat(), mHeight.toFloat(), Path.Direction.CW)
        mPath1.addRoundRect(0f, 0f, mWidth.toFloat(), mHeight.toFloat(), radii, Path.Direction.CW)
        mPath.op(mPath1, Path.Op.XOR)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        mPaint.xfermode = mXfermode
        canvas.drawPath(mPath, mPaint)
    }

    override fun dispatchTouchEvent(event: MotionEvent): Boolean {
        if (!mRegion.contains(event.x.toInt(), event.y.toInt())) {
            return false
        }
        return super.dispatchTouchEvent(event)
    }

}